package com.zjitc.controller;

import com.zjitc.model.Country;
import com.zjitc.service.CountryService;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import sun.misc.Request;

/**
 * Created by tt on 2018/3/20.
 */
@Controller
public class CountryController {
    @Autowired
    private CountryService countryService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List<Country> list=countryService.findAllCountry();
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/country/index.jsp");
        mv.addObject("list",list);
        return mv;
    }
  //add跳转
  @RequestMapping("/addUI")
  public ModelAndView addUI(){
      ModelAndView mv=new ModelAndView();
      mv.setViewName("/country/add.jsp");
      return mv;
    }
    //add
    @RequestMapping("/addAll")
  public ModelAndView addAll(HttpServletRequest request){
    ModelAndView mv=new ModelAndView();
    Country country=new Country();
      try {
        BeanUtils.populate(country,request.getParameterMap());
        int count=countryService.addAll(country);

      } catch (Exception e) {
        e.printStackTrace();
      }
      mv.setViewName("/sakura/findAll");
      return mv;
    }

    //delete
  @RequestMapping("/delete")
  public ModelAndView deleteData(HttpServletRequest request){
    ModelAndView mv=new ModelAndView();
    String code=request.getParameter("code");
    int count=countryService.delete(code);
    mv.setViewName("/sakura/findAll");
    return mv;
  }

  //updateUI
  @RequestMapping("/updateUI")
  public ModelAndView updateUI(HttpServletRequest request){
    ModelAndView mv=new ModelAndView();
    String code=request.getParameter("code");
    Country country=countryService.findByCode(code);
    mv.addObject("c",country);
    mv.setViewName("/country/update.jsp");
    return mv;
  }

  //update
  @RequestMapping("/edit")
  public ModelAndView edit(HttpServletRequest request){
    ModelAndView mv=new ModelAndView();
    Country country=new Country();
    try {
      BeanUtils.populate(country,request.getParameterMap());
      int count=countryService.edit(country);
    } catch (Exception e) {
      e.printStackTrace();
    }
    mv.setViewName("/sakura/findAll");
    return mv;
  }

  //findByCondition
  @RequestMapping("/findByCondition")
  public ModelAndView findByCondition(HttpServletRequest request){
    ModelAndView mv=new ModelAndView();
    String condition=request.getParameter("condition");
    List<Country> list=countryService.findByCondition(condition);
    mv.setViewName("/country/index.jsp");
    mv.addObject("condition",condition);
    mv.addObject("list",list);
    return mv;
  }
}
